﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Xml;

namespace WuhanIns.ConSite.SiteMarker.Core.Util
{
    /// <summary>
    /// XML的缓存处理
    /// </summary>
    public class XmlCache
    {
        /// <summary>
        /// 现场巡检 通过Path（文件名）获取WebApi接口中Xml配置的通用类型
        /// </summary>
        /// <param name="Path"></param>
        /// <returns></returns>
        public static List<TreeListItem> GetCommonTypeNodesByPath(string Path, string GroupName)
        {
            ICacheStrategy strategy = CacheManager.Instance.GetCacheStrategy(ConstArgs.MemCache_ConfigCache);
            List<TreeListItem> entitiys = strategy.Get<List<TreeListItem>>(Path + "_CommonType_RootType_" + GroupName);
            if (entitiys == null)
            {
                entitiys = new List<TreeListItem>();
                string path = Path;
                XmlDocument doc = new XmlDocument();
                if (CommonUtil.FileIsExist(path))
                {
                    doc.Load(path);
                    XmlNode node = doc.SelectSingleNode("/Root/CommonType/RootType[@Name='" + GroupName + "']");
                    if (node != null)
                    {
                        XmlNodeList nodes = node.SelectNodes("Common");
                        if (nodes != null && nodes.Count > 0)
                        {
                            foreach (XmlNode curnode in nodes)
                            {
                                string id = curnode.Attributes["ID"].Value;
                                string Name = curnode.Attributes["Name"].Value;
                                string Code = curnode.Attributes["Code"].Value;
                                var isProjectAttr = curnode.Attributes["IsProjectDept"];
                                string isProject = isProjectAttr == null ? string.Empty : isProjectAttr.Value;
                                var userGroupType = curnode.Attributes["UserGroupType"];
                                string userType = userGroupType == null ? string.Empty : userGroupType.Value;

                                TreeListItem item = new TreeListItem();
                                item.Id = id;
                                item.Text = Name;
                                item.Code = Code;
                                item.Extends["GroupName"] = GroupName;
                                item.Extends["Level"] = 1;
                                item.Extends["IsProjectDept"] = isProject;
                                item.Extends["UserGroupType"] = userType;
                                //获取通用类型的子项
                                XmlNodeList subNodes = curnode.SelectNodes("SubItem[@GroupName='" + Name + "']");
                                if (subNodes != null && subNodes.Count > 0)
                                {
                                    item.Extends["HasChild"] = true;
                                    foreach (XmlNode subNode in subNodes)
                                    {
                                        string subid = subNode.Attributes["ID"].Value;
                                        string subName = subNode.Attributes["Name"].Value;
                                        string subCode = subNode.Attributes["Code"].Value;
                                        var subIsProjectAttr = subNode.Attributes["IsProjectDept"];
                                        string subIsProject = subIsProjectAttr == null ? string.Empty : subIsProjectAttr.Value;
                                        var subUserGroupType = subNode.Attributes["UserGroupType"];
                                        string subUserType = subUserGroupType == null ? string.Empty : userGroupType.Value;

                                        TreeListItem subitem = new TreeListItem();
                                        subitem.Id = subid;
                                        subitem.Text = subName;
                                        subitem.Code = subCode;
                                        subitem.Extends["GroupName"] = Name;
                                        subitem.Extends["Level"] = 2;
                                        subitem.Extends["HasChild"] = false;
                                        subitem.Extends["IsProjectDept"] = subIsProject;
                                        subitem.Extends["UserGroupType"] = subUserType;
                                        entitiys.Add(subitem);
                                    }
                                }
                                else
                                {
                                    item.Extends["HasChild"] = false;
                                }
                                entitiys.Add(item);
                            }
                            strategy.Add(Path + "_CommonType_RootType_" + GroupName, entitiys);
                        }
                    }
                }
            }
            return entitiys;
        }


        /// <summary>
        /// 编码规则 通过Path（文件名）获取WebApi接口中Xml配置的通用类型
        /// </summary>
        /// <param name="Path"></param>
        /// <returns></returns>
        public static CodeRuleItem GetCoderuleNodesByPath(string Path, string TableName, string ColumnName)
        {
            ICacheStrategy strategy = CacheManager.Instance.GetCacheStrategy(ConstArgs.MemCache_ConfigCache);
            CodeRuleItem entitiy = strategy.Get<CodeRuleItem>(Path + "_CodeRule_" + TableName + "_" + ColumnName);
            if (entitiy == null)
            {
                entitiy = new CodeRuleItem();
                XmlDocument doc = new XmlDocument();
                if (CommonUtil.FileIsExist(Path))
                {
                    doc.Load(Path);
                    XmlNode node = doc.SelectSingleNode("/Root/CodeRuleFormat/Table[@Value='" + TableName + "']");
                    if (node != null)
                    {
                        XmlNode columnNode = node.SelectSingleNode("Column[@Value='" + ColumnName + "']");
                        if (columnNode != null)
                        {
                            string PreCode = columnNode.SelectSingleNode("PreCode").InnerText;
                            string DateCode = columnNode.SelectSingleNode("DateFormat").InnerText;
                            string SerialCodeChar = columnNode.SelectSingleNode("SerialCode").InnerText;
                            string SerialCodeLength = columnNode.SelectSingleNode("SerialCode").Attributes["Length"].Value;

                            entitiy = new CodeRuleItem(PreCode, DateCode, new SerialCodeItem(char.Parse(SerialCodeChar), int.Parse(SerialCodeLength)));
                            strategy.Add(Path + "_CodeRule_" + TableName + "_" + ColumnName, entitiy);
                        }
                    }
                }
            }
            return entitiy;
        }


    }
}
